<?php
include (MODEL.'/Repository.php');

class AccountRepository extends Repository
{
    private $_user;
    
	public function __construct()
	{
		parent::__construct();
	}	

	public function CheckUser($email) 
	{
		$collection = $this->DB->selectCollection('Account');
		$cond = array('Email' => $email);

		$reg = $collection->findOne($cond);
		if(empty($reg))
			return false;
		else	
			return true;
	}
	
	public function Register($model) 
	{		
		$this->collection = $this->DB->selectCollection('Account');
		
		$dateInfo = date_parse_from_format('m/d/Y', $model->Birthday);
		$birthday = mktime($dateInfo['month'], $dateInfo['day'], $dateInfo['year']);
		
		$value = array(					
					'Email' => $model->Email,
					'Password' => md5($model->Password),
					'FirstName' => $model->FirstName,					
					'LastName' => $model->LastName,
					'Birthday' => new MongoDate($birthday),
					'Sex' => $model->Sex,
					'CreateDate' => new MongoDate()
				 );
		
		$this->collection->insert($value);
	}
	
    public function CheckLogin($email, $password)
    {                
        $collection = $this->DB->selectCollection('Account');        
        $query = array('Email' => $email, 'Password' => md5($password));

        $this->_user = $collection->findOne($query);
        if (empty($this->_user)) return false;

        $_SESSION['user_id'] = (string)$this->_user['_id'];

        return true;
    }
    
    public function Logout()
    {
        unset($_SESSION['user_id']);
    }
    
    public function Delete()
    {
    	$collection = $this->DB->selectCollection('Account');
    	
    	$query = array('_id'=>'4fdee32a6803fa610b000000');
    	$item = $collection->remove($query,true);
    }
}